/**
 * Created by gaoyj on 2018/6/27.
 */
window.onload = function (ev) {
    topbar.hide();
};
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";
$("#startTime").val(_start);
$("#endTime").val(_end);
laydate.render({
    elem: '#startTime',
    type: 'datetime',

});
laydate.render({
    elem: '#endTime' //指定元素
    , type: 'datetime',
});
$("#addForm").validate({
    rules: {
        name: {
            required: true,
            minlength: 2
        },
        phoneNo: {
            required: true
        },
        loginPwd: {
            minlength: 6,
            maxlength: 20
        }
    }
});
$(function () {
    $.ajax({
        type: "POST",
        url: "/CardProductController/queryAllCardOperatorList",
        dataType: "json",
        success: function (data) {
            $("#productType").html("");
            var str = "<option value=\"\">运营商</option>";
            if (data != null && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    str += '<option value=' + data[i].cardOperatorId + '>' + data[i].cardOperatorName + '</option>'
                }
            }
            $("#productType").append(str);
        },
        beforeSend: function () {
        }
    });
})
$("#dataTable").bootstrapTable({
    url: '/CardReceiveController/cardDetailByBatchId',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: true,
    pageList: [30, 50, 100],
    clickToSelect: true,
    queryParams: queryParams,
    uniqueId: 'id',
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,//总页数
            "rows": res.resultList   //数据
        };
    },
    columns: [[
        {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        }, {
            width: '40px',
            align: 'center',
            field: 'cardNumber',
            title: '卡号'
        }, {
            width: '40px',
            align: 'center',
            field: 'startTime',
            title: '收卡时间'
        }, {
            width: '40px',
            align: 'center',
            field: 'endTime',
            title: '销卡时间'
        }, {
            width: '40px',
            align: 'center',
            field: 'effectTime',
            title: '失效时间',formatter:function (v,o) {
                return getEffect(o.effectTime);
            }
        }, {
            field: 'timeElapse',
            title: '销卡耗时(秒)',
            width: 75,
            align: 'center',
            formatter: function (v, o) {
                if (o.status == 1) {
                    return (new Date(o.endTime).getTime() - new Date(o.startTime).getTime()) / 1000
                } else {
                    return '--';
                }
            }
        }, {
            width: '40px',
            align: 'center',
            field: 'cardOperatorName',
            title: '运营商'
        }, {
            width: '40px',
            align: 'center',
            field: 'status',
            title: '状态',
            formatter: function (v, row, index) {
                return convertStatus(v)
            }
        }, {
            width: '40px',
            align: 'center',
            field: 'userPrice',
            title: '折扣'
        }, {
            width: '40px',
            align: 'center',
            field: 'face',
            title: '收卡面值'
        }, {
            width: '40px',
            align: 'center',
            field: 'settlePrice',
            title: '已销卡面值'
        }, {
            width: '40px',
            align: 'center',
            field: 'judgeFace',
            title: '预判面值'
        }, {
            width: '40px',
            align: 'center',
            field: 'failReason',
            title: '失败原因',
            formatter: function (v, o) {
                if(/^[0-9]+$/.test(v)){
                    return _getCardFailReason(v);
                }else{
                    return v;
                }
            }
        }
    ]]
})

$("#search").click(function () {
    var timeAdd = new Date($("#startTime").val());
    var timeEnd = new Date($("#endTime").val());
    if (timeAdd > timeEnd) {
        layer.msg("开始时间不能大于结束时间");
        return false;
    }
    var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
    if (require > 7) {
        layer.msg("时间间隔不能超过7天")
        return;
    }
    $("#dataTable").bootstrapTable("refresh");
});

function queryParams(params) {
    return {
        page: params.pageNumber,
        size: params.pageSize,
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        batchId: $.trim($("#batchId").val()),
        face: $.trim($("#face").val()),
        status: $("#state").val(),
        productType: $("#productType").val()
    };
}
function getEffect(value) {
    return value.substring(0, 4) + "-" + value.substring(4, 6) + "-" + value.substring(6, 8) + " " + value.substring(8, 10) + ":" + value.substring(10, 12) + ":" + value.substring(12, 14)
}
